using System;
using System.Collections.Generic;
using System.Text;
using OlympusSoftware.Technical.Globalize.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace OlympusSoftware.Technical.Globalize.BusinessLogic
{
    public class GlobalizeProviderSql : IGlobalizeProvider
    {
        private static Database GetDatabase()
        {
            return DatabaseFactory.CreateDatabase("OlympusSoftware");
        }
        #region IGlobalizeProvider Members

        public List<Language> GetLanguagesForSite(int siteId)
        {
            Database olympusBd = GetDatabase();
            DbCommand cmd = olympusBd.GetStoredProcCommand("[dbo].[GlobalizeGetLanguages]");
            olympusBd.AddInParameter(cmd, "@IdSite", System.Data.DbType.Int32, siteId);
            List<Language> languages = null;
            using (IDataReader reader = olympusBd.ExecuteReader(cmd))
            {
                languages = new List<Language>();

                while (reader.Read())
                {
                    languages.Add( new Language(reader.GetInt32(0), reader.GetString(1)) );
                }
            }
            return languages;
        }

        public string GetText(int siteId, int pageId, int languageId, int infoId)
        {
            Database olympusBd = GetDatabase();
            DbCommand cmd = olympusBd.GetStoredProcCommand("[dbo].[GlobalizeGetText]");
            olympusBd.AddInParameter(cmd, "@IdSite", System.Data.DbType.Int32, siteId);
            olympusBd.AddInParameter(cmd, "@IdPage", System.Data.DbType.Int32, pageId);
            olympusBd.AddInParameter(cmd, "@IdInfo", System.Data.DbType.Int32, infoId);
            olympusBd.AddInParameter(cmd, "@IdLanguage", System.Data.DbType.Int32, languageId);
            string res = null;
            using (IDataReader reader = olympusBd.ExecuteReader(cmd))
            {
                while (reader.Read())
                {
                    res = reader.GetString(0);
                }
            }
            return res;
        }

        #endregion
    }
}

